package com.wongleoi.weather_demo

import android.graphics.Paint
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.wongleoi.weather_demo.databinding.WeatherItemLayoutBinding
import com.wongleoi.weather_demo.model.CityBean

class CityWeatherListAdapter(private val data: MutableList<CityBean>) :
    RecyclerView.Adapter<CityWeatherViewHolder>() {

    private var itemClickListener: ((CityBean) -> Unit)? = null

    val adapterData get() = data

    fun setOnItemClickListener(itemClickListener: (CityBean) -> Unit) {
        this.itemClickListener = itemClickListener
    }

    override fun onBindViewHolder(holder: CityWeatherViewHolder, position: Int) {
        holder.bind(data[position])
        holder.viewBinding.root.setOnClickListener {
            itemClickListener?.invoke(data[position])
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CityWeatherViewHolder {
        return CityWeatherViewHolder(
            WeatherItemLayoutBinding.inflate(
                LayoutInflater.from(parent.context),
                parent,
                false
            )
        )
    }

    override fun getItemCount() = data.size

}

class CityWeatherViewHolder(val viewBinding: WeatherItemLayoutBinding) :
    RecyclerView.ViewHolder(viewBinding.root) {
    fun bind(item: CityBean) {
        viewBinding.tvCity.run {
            text = item.cityName
            paint.flags=Paint.UNDERLINE_TEXT_FLAG
            paint.isAntiAlias=true
        }

    }

}


